package com.duobaoyu.dby.supply.admin.controller.risk;

import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.converter.BalanceWarnConverter;
import com.duobaoyu.dby.supply.biz.service.config.BalanceWarnService;
import com.duobaoyu.dby.supply.common.biz.bo.config.BalanceWarnBO;
import com.duobaoyu.dby.supply.common.biz.dto.config.BalanceWarnBatchDTO;
import com.duobaoyu.dby.supply.common.biz.dto.config.BalanceWarnDTO;
import com.duobaoyu.dby.supply.common.biz.vo.config.BalanceWarnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 余额预警
 *
 * @author yanhan
 * @version 1.0
 * @date 2022/12/22 18:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/balance-emergy")
@Api(value = "balance-emergy", tags = "余额预警")
public class BalanceWarnController {

	private final BalanceWarnService balanceWarnService;
	private final BalanceWarnConverter balanceWarnConverter;

	/**
	 * 修改余额预警
	 * @param id id
	 * @param dto dto
	 * @return R<Boolean>
	 */
	@PostMapping("/update/{id}")
	@ApiOperation(value = "修改余额预警", notes = "修改余额预警")
	@SupplySysLog(value = "修改余额预警",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> update(@NotNull(message = "余额预警ID不能为空") @PathVariable("id") Long id, @RequestBody @Valid BalanceWarnDTO dto) {
		dto.setId(id);
		return R.ok(balanceWarnService.modify(dto));
	}

	/**
	 * 保存余额预警
	 * @param dto dto
	 * @return R<Boolean>
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存余额预警", notes = "保存余额预警")
	@SupplySysLog(value = "保存余额预警",operationType = OperationTypeEnum.INTERT)
	public R<Boolean> save(@RequestBody @Valid BalanceWarnBatchDTO dto) {
		return R.ok(balanceWarnService.batchSave(dto));
	}

	/**
	 * 查询余额预警信息
	 * @return R<BalanceWarnVO>
	 */
	@GetMapping("/query")
	@ApiOperation(value = "查询余额预警信息", notes = "查询余额预警信息")
	@SupplySysLog(value = "查询余额预警信息",operationType = OperationTypeEnum.SELECT)
	public R<BalanceWarnVO> query() {
		BalanceWarnBO balanceWarn = balanceWarnService.queryBalanceWarn();
		return R.ok(balanceWarnConverter.toBalanceWarn(balanceWarn));
	}
}
